import React, { Component } from 'react'
import Taro,{Current} from '@tarojs/taro'
import { View } from '@tarojs/components'
import {DoctorThing} from '../../../components/view/DoctorThing'
import {TimeThings} from '../../../components/view/TimeThings'
import {api_get_doctor_things,api_get_doctor_times } from '../../../utils/api'

export default class selectTime extends Component {

  constructor(){
    super(...arguments)
    this.state = {
      doctor_loading:false,
      doctor_data:'',
      time_loading:false,
      time_data:'',
      getting:false
    }
  }
  
  componentWillMount(){
    this.setState({
      doctor_id:Current.router.params.doctor_id,
      getting:true
    })
    Taro.showLoading({
      title: '加载中',
    })
  }

  myRequest =  () => {
    const doctor_params = {
      url: api_get_doctor_things,
      data: {
        doctor_id:this.state.doctor_id,
      },
      method: 'POST',
      header: {'content-type': 'application/x-www-form-urlencoded'},
      success: (res) => {
        this.setState({
          doctor_data:res.data,
          doctor_loading:true
        })  
      },
      fail: (res) => {console.log(res.data)}
    }
    const time_params = {
      url: api_get_doctor_times,
      data: {
        doctor_id:this.state.doctor_id,
      },
      method: 'POST',
      header: {'content-type': 'application/x-www-form-urlencoded'},
      success: (res) => {
        console.log(res);
        
        this.setState({
          time_data:res.data,
          time_loading:true
        })  
      },
      fail: (res) => {console.log(res.data)}
    }
    Taro.request(doctor_params)
    Taro.request(time_params)
  }


  render () {
    if(this.state.getting && !this.state.doctor_loading && !this.state.time_loading){
      this.myRequest()
    }
    if(this.state.doctor_loading && this.state.time_loading){
      setTimeout(function () {Taro.hideLoading()}, 100)   
    }
    return (
      this.state.doctor_loading && this.state.time_loading?
      <View className='selectTime'>
        <DoctorThing data={this.state.doctor_data}></DoctorThing>
        <TimeThings data={this.state.time_data} doctor_name={this.state.doctor_data.doctor_name}
          fee={this.state.doctor_data.order_fee}></TimeThings>
      </View>
      :<View></View>
    )
  }
}
